% fname: buffer_8__routing_dyad_0_6__sel_bufferlevel__topology_8x8__.m
% ./noxim -buffer 8 -routing dyad 0.6 -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = buffer_8__routing_dyad_0_6__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0018        18.7589      0.0155676            110            950           7597
           0.0018        16.1027      0.0146885             94            896           7168
           0.0018         16.983      0.0151452             91            940           7512
           0.0018        16.5473      0.0147698             97            930           7444
           0.0018        17.5582       0.014666             97            894           7157
           0.0018        15.6399      0.0150343             81            933           7457
           0.0018        15.7352      0.0139668             70            895           7151
           0.0018        16.1649      0.0152903             64            946           7584
           0.0018        16.6422      0.0147016             76            911           7292
           0.0018        16.5765      0.0147361            114            928           7427
           0.0018        16.4528      0.0150521             93            901           7225
           0.0018         17.427      0.0150369            117            918           7338
           0.0018        16.3477       0.014998             85            929           7439
           0.0018        15.9989      0.0155826             72            919           7355
           0.0018        16.5729      0.0146209             89            892           7135
           0.0018        16.6232      0.0154859             82            958           7681
           0.0018         16.043      0.0143948             82            907           7255
           0.0018        17.4707      0.0156059            103            922           7366
           0.0018        16.0047      0.0143157             82            845           6757
           0.0018        17.0181      0.0149187            108            940           7519
            0.002         16.971      0.0158299            107            966           7725
            0.002        16.9322      0.0161389             81           1017           8134
            0.002        18.6648      0.0163691             84           1047           8381
            0.002        16.7241      0.0157421             97            993           7934
            0.002        18.0773      0.0160139            114           1009           8071
            0.002        17.7213       0.015871             80           1001           7999
            0.002         16.967      0.0170066             77            969           7755
            0.002        17.9178      0.0175219             98            998           7990
            0.002        18.7317      0.0165099            137           1040           8321
            0.002        18.6693      0.0163492            122           1031           8240
            0.002        17.8815      0.0170205            109           1038           8306
            0.002        16.9503      0.0170466             92           1006           8046
            0.002        17.0732      0.0172958            100           1038           8302
            0.002        17.9554      0.0162601            169           1008           8065
            0.002         18.172      0.0167036             97           1035           8285
            0.002        18.0366      0.0160516             95           1011           8090
            0.002        17.9951      0.0166906            140           1017           8145
            0.002         19.844       0.017381            116           1096           8760
            0.002          18.95      0.0173167            134           1039           8312
            0.002         17.251      0.0166167             99            996           7976
           0.0022        19.4267      0.0197746            117           1207           9650
           0.0022        18.8293      0.0170972            107           1078           8617
           0.0022        20.1758      0.0185298            115           1166           9339
           0.0022        19.3915      0.0186726            142           1175           9411
           0.0022        19.5098      0.0178234             89           1124           8983
           0.0022        19.3934      0.0177282            185           1116           8935
           0.0022        17.5798       0.017334             87           1109           8875
           0.0022        17.9664      0.0184028            102           1159           9275
           0.0022        19.3391      0.0188934             97           1153           9220
           0.0022        18.6534      0.0177051            104           1131           9065
           0.0022        19.2697      0.0175927            115           1090           8726
           0.0022        20.5854       0.017377            149           1112           8897
           0.0022        19.2772      0.0178242            114           1140           9126
           0.0022        20.6395      0.0191504            178           1129           9039
           0.0022        19.0018      0.0178532            122           1125           8998
           0.0022         18.128      0.0178891             89           1109           8873
           0.0022        18.7414      0.0173347            116           1075           8598
           0.0022        19.0418      0.0188831             89           1171           9366
           0.0022        17.6864      0.0176786            115           1113           8910
           0.0022        18.0027      0.0179093             98           1109           8883
           0.0024          18.33      0.0186055            146           1191           9526
           0.0024        20.4635       0.019043            106           1219           9750
           0.0024        19.9085      0.0201429            109           1268          10152
           0.0024        18.7884      0.0194456            105           1205           9645
           0.0024        19.7371      0.0194385            133           1225           9797
           0.0024        22.9132       0.019793            179           1267          10134
           0.0024        23.2325      0.0199508            196           1217           9736
           0.0024        22.0763      0.0190449            161           1219           9751
           0.0024        21.7211      0.0195377            199           1230           9847
           0.0024         19.925      0.0186369             96           1174           9393
           0.0024         22.965      0.0210799            133           1287          10287
           0.0024        19.6356      0.0186915            127           1158           9271
           0.0024        19.7879      0.0194315             92           1207           9638
           0.0024        19.3763      0.0188197            192           1148           9184
           0.0024        20.3078      0.0194467            138           1186           9490
           0.0024        22.0205      0.0198008            202           1267          10138
           0.0024        21.0845      0.0190887            142           1184           9468
           0.0024        22.8918       0.019256            225           1211           9705
           0.0024        20.8978      0.0191113            136           1223           9785
           0.0024        20.1715      0.0196171            136           1236           9887
           0.0026        20.8513      0.0209435            121           1298          10388
           0.0026        22.1863      0.0208164            135           1331          10658
           0.0026        23.2814      0.0208611            182           1315          10514
           0.0026        29.3197      0.0214512            289           1373          10983
           0.0026        22.1088      0.0217222            189           1369          10948
           0.0026        22.8976      0.0210352            131           1347          10770
           0.0026        22.1305      0.0200801            284           1287          10281
           0.0026        23.0537       0.020082            156           1284          10282
           0.0026        23.9411      0.0204861            172           1291          10325
           0.0026        23.7302      0.0213301            177           1364          10921
           0.0026        23.8346      0.0213669            233           1324          10598
           0.0026         21.216       0.020875            145           1315          10521
           0.0026         24.324       0.021996            199           1364          10910
           0.0026        21.7605      0.0211587            176           1332          10664
           0.0026        24.2447      0.0223452            193           1406          11262
           0.0026        21.1497       0.020459            112           1309          10475
           0.0026         23.292      0.0203066            173           1298          10397
           0.0026        22.1094      0.0202773            150           1298          10382
           0.0026         20.917      0.0212969            190           1361          10904
           0.0026        29.3678      0.0222639            337           1403          11221
           0.0028        23.8051      0.0219805            154           1406          11254
           0.0028        26.4827      0.0221387            208           1419          11335
           0.0028        24.8525      0.0218496            216           1397          11187
           0.0028        25.6216      0.0231406            158           1480          11848
           0.0028        23.9484      0.0233591            124           1472          11773
           0.0028        25.1135      0.0218333            261           1374          11004
           0.0028        25.9834      0.0225332            217           1442          11537
           0.0028        30.3282      0.0241895            342           1499          11998
           0.0028        24.0986      0.0218909            144           1379          11033
           0.0028        22.8406      0.0213828            161           1368          10948
           0.0028        30.0102      0.0230078            314           1473          11780
           0.0028        25.0185      0.0226835            288           1407          11251
           0.0028        24.7574      0.0223184            173           1426          11427
           0.0028        21.6375      0.0222817            111           1404          11230
           0.0028         23.665      0.0217539            162           1391          11138
           0.0028        23.6594      0.0220766            139           1368          10950
           0.0028        25.7696       0.021959            175           1406          11243
           0.0028        23.5471      0.0218672            144           1400          11196
           0.0028        26.9131       0.022834            198           1461          11691
           0.0028        26.0261      0.0232602            213           1420          11351
            0.003        25.6079       0.023043            182           1474          11798
            0.003        26.7901       0.023998            222           1534          12287
            0.003         33.685      0.0242188            381           1549          12400
            0.003        25.3534      0.0234199            142           1497          11991
            0.003        26.6161      0.0234127            289           1477          11800
            0.003        26.2534      0.0238691            182           1527          12221
            0.003         27.094      0.0234524            263           1478          11820
            0.003        29.6436      0.0239844            335           1535          12280
            0.003        26.9702      0.0239405            213           1509          12066
            0.003        27.1417      0.0228437            249           1461          11696
            0.003        27.6373      0.0235898            242           1511          12078
            0.003        31.7015      0.0245645            247           1571          12577
            0.003        26.5553      0.0242817            308           1529          12238
            0.003        27.4128      0.0240938            215           1543          12336
            0.003        37.6569      0.0245625            355           1574          12576
            0.003        28.0553      0.0232917            330           1466          11739
            0.003        27.8697      0.0243571            183           1535          12276
            0.003         38.507      0.0245059            460           1568          12547
            0.003        29.1128      0.0240898            216           1542          12334
            0.003        28.0495      0.0236641            375           1515          12116
           0.0032        34.9547      0.0251992            415           1612          12902
           0.0032        32.5302      0.0263418            240           1686          13487
           0.0032        32.0421       0.027084            212           1733          13867
           0.0032        34.5585      0.0256543            226           1642          13135
           0.0032        33.1253      0.0258066            251           1652          13213
           0.0032        32.6044      0.0249883            197           1600          12794
           0.0032        36.8365      0.0263691            449           1688          13501
           0.0032        28.9029       0.025002            276           1575          12601
           0.0032        31.0396       0.025627            219           1615          12916
           0.0032        36.3501      0.0269687            494           1725          13808
           0.0032        36.1004       0.025373            392           1623          12991
           0.0032        41.1208      0.0260059            501           1664          13315
           0.0032        45.7962      0.0268552            634           1693          13535
           0.0032        27.2894      0.0241758            188           1548          12378
           0.0032        45.5891      0.0261953            709           1677          13412
           0.0032        33.6136      0.0244004            497           1563          12493
           0.0032        33.5146      0.0259415            319           1607          12867
           0.0032         32.677      0.0257617            260           1650          13190
           0.0032        39.6111      0.0261289            428           1674          13378
           0.0032        34.6412      0.0248047            320           1586          12700
           0.0034        42.8546      0.0276016            332           1767          14132
           0.0034        38.0012      0.0277238            285           1718          13751
           0.0034         36.574      0.0274336            287           1756          14046
           0.0034        44.1388      0.0274922            596           1758          14076
           0.0034        48.2652      0.0269941            852           1727          13821
           0.0034        45.6874      0.0273301            572           1750          13993
           0.0034        39.7819      0.0265703            548           1701          13604
           0.0034        48.1272      0.0280655            567           1769          14145
           0.0034        51.2342      0.0271895            738           1742          13921
           0.0034         49.619      0.0282852            472           1811          14482
           0.0034        44.0397      0.0255469            686           1636          13080
           0.0034        39.9976      0.0263398            921           1687          13486
           0.0034         43.544       0.027168            443           1739          13910
           0.0034        51.6062      0.0276973            785           1770          14181
           0.0034        55.5873      0.0267637            956           1713          13703
           0.0034        44.4355      0.0267734            391           1713          13708
           0.0034        55.3855      0.0276309            726           1769          14147
           0.0034        40.8645      0.0269702            521           1697          13593
           0.0034         76.995      0.0281484            811           1802          14412
           0.0034        42.2311      0.0270488            339           1731          13849
           0.0036         233.76      0.0296627           2927           1869          14950
           0.0036        99.1736      0.0294277           1464           1884          15067
           0.0036        49.6363      0.0289512            445           1853          14823
           0.0036        46.5272      0.0284121            379           1817          14547
           0.0036         53.988        0.02875            654           1840          14720
           0.0036        50.5008      0.0290898            439           1861          14894
           0.0036        67.0059      0.0289844           1013           1855          14840
           0.0036        54.5053      0.0279473            602           1789          14309
           0.0036        168.755      0.0300098           2819           1921          15365
           0.0036        57.1086      0.0289082            713           1851          14801
           0.0036        61.0988      0.0292695            869           1873          14986
           0.0036        46.7175      0.0280879            591           1798          14381
           0.0036        52.1868      0.0281133            749           1799          14394
           0.0036        59.8271      0.0286329            732           1805          14431
           0.0036        59.9065      0.0287344            507           1839          14712
           0.0036        54.1093      0.0284453            579           1821          14564
           0.0036        39.8935      0.0281687            608           1774          14197
           0.0036        68.9368      0.0286465           1416           1835          14667
           0.0036         47.431      0.0280645            485           1796          14369
           0.0036        69.1599      0.0302718            691           1908          15257
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
